package com.xncoding.tomcat.socket;
import java.io.*;
import java.net.*;

public class Client {

    public static void main(String[] args) throws Exception {
        String readline;
        String inTemp;
        final String client = "客户端说:";
        final String server = "服务端回复:";

        int port = 8090;
        byte[] ipAddressTemp = {127, 0, 0, 1};
        InetAddress ipAddress = InetAddress.getByAddress(ipAddressTemp);

        //首先直接创建socket,端口号1~1023为系统保存，一般设在1023之外
        Socket socket = new Socket(ipAddress, port);

        BufferedReader systemIn = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter socketOut = new PrintWriter(socket.getOutputStream());
        while (true) {
            System.out.print(client);
            readline = systemIn.readLine();

            socketOut.println(readline);
            socketOut.flush();
            //处理
            inTemp = socketIn.readLine();
            if (inTemp != null && inTemp.contains("over")) {
                systemIn.close();
                socketIn.close();
                socketOut.close();
                socket.close();
            }
            System.out.println(server + inTemp);
        }
    }
}
